(function() {

	function Base() {
		this.storage = plus.storage;
	};
	//数据更新列表
	Base.prototype.getDataUpdate = function() {
		var updateData = this.storage.getItem("updateData");
		if(updateData) {
			return JSON.parse(updateData);
		} else {
			return {
				InventoryAsset: {
					icon: "folder",
					title: '盘点总资产数据',
					update_time: '从未更新'
				},
				InventoryList: {
					icon: "folder",
					title: '盘点任务数据',
					update_time: '从未更新'
				},
			};
		}

	};
	Base.prototype.SetDataUpdate = function(data) {
		this.storage.setItem("updateData", JSON.stringify(data));
	};

	/***盘点任务列表Start***/
	//盘点数量
	var InventoryAssetListArr = 0;
	//盘点列表总数
	var InventoryAssetListTotal = 0;
	Base.prototype.fetchInventoryAssetList = function() {
		var _this = this;
		app.request.json("http://" + plus.storage.getItem('locationHref') + "/Stock_Check/Document_List_Query?IsFinished=false&_r=813efd8e-1ac0-4cc8-8c35-3d502f680353", {
			pageSize: 9999
		}, function(data) {
			if(eval(data.rows).length == 0) {
//				app.dialog.alert("无盘点单数据", function() {
					$$("#InventoryList").trigger('InventoryAssetListEnd', new Date().toLocaleString(), "无盘点单数据");
					InventoryAssetListTotal = null;
					InventoryAssetListArr = null;
					_this.storage.setItem("InventoryAssetList","");
//				});


			} else {
				_this.setInventoryAssetList(data);
			}

		}, function(xhr, status) {
			console.log(status + "-------fetchInventoryAssetList")
		});
	};
	Base.prototype.setInventoryAssetList = function(data, judge) {
		if(judge) {
			var _data = data;
		} else {
			var _data = window.dataHandle(data);
			//房间信息读取(子读取3)
			this.fetchRoomInformation();
			_data.forEach((ele, index) => {
				//盘点任务列表标题信息(子读取1)
				this.fetchInventoryAssetListTitle(_data[index].Id);
				//盘点任务列表内资产(子读取2)
				this.fetchInventoryAssetListContent(_data[index].Id);
			});
		};
		InventoryAssetListTotal = _data.length;
		this.storage.setItem("InventoryAssetList", JSON.stringify(_data));
		if(judge) {
			$$("a.qrUpload").trigger("setInventoryAssetListDone", {
				success: true,
				message: "保存成功!"
			});
		};
	};
	Base.prototype.getInventoryAssetList = function() {
		var _data = this.storage.getItem("InventoryAssetList");
		if(_data){
			return JSON.parse(_data);
		}
	};
	/***盘点任务列表End***/
	/*
	 *盘点任务列表标题信息Start
	 *子读取1
	 */
	Base.prototype.fetchInventoryAssetListTitle = function(id) {
		var _this = this;
		app.request.json("http://" + plus.storage.getItem('locationHref') + "/Stock_Check/Document_Get_Model?_r=813efd8e-1ac0-4cc8-8c35-3d502f680353", {
				id: id
			}, function(data) {
				_this.setInventoryAssetListTitle(data.model, id);
			},
			function(xhr, status) {
				console.log(status + "-----fetchInventoryAssetListTitle");
			});
	};
	Base.prototype.setInventoryAssetListTitle = function(data, id) {
		this.storage.setItem("InventoryAssetListTitle" + id, JSON.stringify(data));

	};
	Base.prototype.getInventoryAssetListTitle = function(id) {
		var _data = this.storage.getItem("InventoryAssetListTitle" + id);
		return JSON.parse(_data);
	};
	/*
	 *盘点任务列表标题信息End
	 * 
	 */

	/*
	 * 盘点任务列表内资产Start
	 * 子读取2
	 */
	Base.prototype.fetchInventoryAssetListContent = function(id) {
		var _this = this;
		app.request({
			url: "http://" + plus.storage.getItem('locationHref') + "/Stock_Check/Document_Get_Detail_List?_r=813efd8e-1ac0-4cc8-8c35-3d502f680353",
			crossDomain: true,
			dataType: "text",
			data: {
				DocumentId: id,
				pageSize: 99999
			},
			success: function(data) {
				_this.setInventoryAssetListContent(id, JSON.parse(data));
			},
			error: function(xhr, status) {
				console.log(status + "-----fetchInventoryAssetListContent");
			}
		});
	};
	Base.prototype.setInventoryAssetListContent = function(id, data, judeg) {
		if(judeg) {
			var _data = data;
		} else {
			var _data = window.dataHandle(data);
		}
		this.storage.setItem("InventoryAssetListContent" + id, JSON.stringify(_data));
		InventoryAssetListArr++;
		if(InventoryAssetListTotal <= InventoryAssetListArr) {
			$$("#InventoryList").trigger('InventoryAssetListEnd', new Date().toLocaleString(), "更新成功");
			InventoryAssetListTotal = null;
			InventoryAssetListArr = null;
		};
	};
	Base.prototype.getInventoryAssetListContent = function(id, params) {
		var _data = JSON.parse(this.storage.getItem("InventoryAssetListContent" + id));
		if(_data) {
			if(params) {
				var _tmp = [];
				for(var i in _data) {
					if(_data[i]['盘点状态'] === params['盘点状态']) {
						_tmp.push(_data[i]);
					}
				};
				return _tmp;
			} else {
				return _data;
			}
		} else {
			return false;
		}

	};
	//盘点任务列表内某个资产
	Base.prototype.getInventoryAssetListContentId = function(id, inventoryCode) {
		var data = this.getInventoryAssetListContent(id);
		if(data) {
			for(var i in data) {
				if(data[i]['AssetId'] === inventoryCode) {
					if(data[i]['盘点结果'] == "盘盈") {
						return false;
					} else {
						return data[i];
					};
				};
			};
		} else {
			return false;
		};
	};
	//盘点任务列表内某个资产更新
	Base.prototype.updateInventoryAssetListContentId = function(id, inventoryCode, data) {
		var _data = this.getInventoryAssetListContent(id);
		var _item = [];
		var _i;
		for(var i in _data) {
			if(inventoryCode === _data[i]['AssetId']) {
				_item = _data[i];
				_i = i;
			};
		};
		if(data) {
			for(var j in data) {
				_item[j] = data[j];
			};
			_data[_i] = _item;
			this.setInventoryAssetListContent(id, _data, true);
		};

	};
	//盘点任务列表外某个资产创建新的
	Base.prototype.createInventoryAssetListContentId = function(id, data, state) {
		var _data = this.getInventoryAssetListContent(id);
		for(var i in _data){
			if(_data[i]['Id'] == data['Id']){			
				_data[i] = data;
				this.setInventoryAssetListContent(id, _data, true);
				return false;
			};
		};
		if(state == "盘盈") {
			_data.push(data);
		};
		this.setInventoryAssetListContent(id, _data, true);
	};
	//盘点任务列表外整个资产本地保存
	Base.prototype.localInventoryAssetList = function(id) {
		var _this = this;
		var _data = _this.getInventoryAssetList();
		for(var i in _data) {
			if(_data[i]['Id'] == id) {
				_data[i]['盘点状态'] = '已盘';
			};
		};
		_this.setInventoryAssetList(_data, true);
		var _dataContent = _this.getInventoryAssetListContent(id);
		for(var j in _dataContent) {
			_dataContent[j]["盘点状态"] = "已盘";
		};

		_this.setInventoryAssetListContent(id, _dataContent, true);

	};
	/*
	 * 盘点任务列表内资产End
	 */

	/*
	 * 房间信息读取Start
	 * 子读取3
	 */
	Base.prototype.fetchRoomInformation = function() {
		var _this = this;
		app.request.json("http://" + plus.storage.getItem('locationHref') + "/Label_Print/Query_Store?_r=2c9e783c-ea72-4055-85a3-f82b342d5fff", {
				pageSize: 9999
			}, function(data) {
				_this.setRoomInformation(data);
			},
			function(xhr, status) {
				console.log(status + "------fetchRoomInformation");
			});
	};
	Base.prototype.setRoomInformation = function(data) {
		var _data = window.dataHandle(data);
		this.storage.setItem('RoomInformation', JSON.stringify(_data));
	};
	Base.prototype.getRoomInformation = function(id) {
		var _data = JSON.parse(this.storage.getItem('RoomInformation'));
		for(var i in _data) {
			if(_data[i]['房间编码'] == id) {
				return _data[i]['房间名称'];
			}
		}
	};
	/*
	 * 房间信息读取End
	 */

	/***盘点任务资产Start***/
	Base.prototype.fetchInventoryAsset = function() {
		var _this = this;
		app.request.json("http://" + plus.storage.getItem('locationHref') + "/Report/APPAsset_Detail", {
			UserCode: plus.storage.getItem("inventoryname") || plus.storage.getItem("username")
		}, function(data) {
			_this.setInventoryAsset(data);
		}, function(xhr, status) {
			console.log(status + "-------fetchInventoryAsset")
		});
	};
	Base.prototype.setInventoryAsset = function(data) {
		var _this = this;
		var _data = window.dataHandle(data);

		console.log(_data.length + "-----setInventoryAsset");
		if(_data.length == 0) {
			console.log("服务器异常" + "-------setInventoryAsset");
			$$('#InventoryAsset').trigger('InventoryAssetEnd', new Date().toLocaleString());
			return false;
		};
		
		//indexedDb数据库
		// 如果不存在Users对象仓库则创建
		if(db.objectStoreNames.contains('AssetInfo')) {
			var store = db.transaction("AssetInfo",'readwrite').objectStore("AssetInfo");
        	store.clear();
		};
		
		var tx = window.db.transaction('AssetInfo', 'readwrite');
		var store = tx.objectStore('AssetInfo');
		for(var i in _data){
			(function(arg){
				var req = store.put(_data[arg]);
				req.onsuccess = function() {
					if(i==arg){
						console.log("保存成功"+new Date().toLocaleString()+"===="+i);
						$$('#InventoryAsset').trigger('InventoryAssetEnd', new Date().toLocaleString());
					}
				};
			})(i);
		};
	};
	/***盘点任务资产End***/
	//提交盘点单
	Base.prototype.pushInventoryAssetList = function(id) {
		var _this = this;
		var _dataContent = _this.getInventoryAssetListContent(id);
		var _newDataArr = [];
		for(var i in _dataContent) {
			var _newDataObj = {};
			_newDataObj.Id = _dataContent[i].Id;
			_newDataObj.AssetId = _dataContent[i].AssetId;
			if(_dataContent[i]["盘点状态"] == "已盘" && _dataContent[i]["盘点结果"] != "未盘") {
				_newDataObj.StoreCode = _dataContent[i].StoreCode;
				_newDataObj['位置编码'] = _dataContent[i].StoreCode;
			} else {
				_newDataObj.StoreCode = null;
				_newDataObj['位置编码'] = null;
			};
			_newDataObj['资产编码'] = _dataContent[i]['资产编码'];
			_newDataObj['盘点编码'] = _dataContent[i]['资产编码'];
			_newDataObj['盘点数量'] = _dataContent[i]['盘点数量'];
			_newDataObj['盘点人'] = plus.storage.getItem("inventoryname") || plus.storage.getItem("username") ;
			_newDataObj['盘点状态'] = _dataContent[i]['盘点状态'];
			_newDataObj['盘点结果'] = _dataContent[i]['盘点结果'];
			_newDataObj['盘点异常'] = _dataContent[i]['盘点异常'];
			_newDataObj['盘点备注信息'] = _dataContent[i]['盘点备注信息'];
			_newDataArr.push(_newDataObj);
		};

		app.request({
			url: "http://" + plus.storage.getItem('locationHref') + "/Stock_Check/AppDocument_SaveFinished",
			crossDomain: true,
			data: {
				Id: id,
				data: _newDataArr
			},
			type: "POST",
			dataType: "json",
			cache: false,
			success: function(response) {
				if(response['responseState'] == "保存成功") {
					var offlineStocktak = _this.getInventoryAssetList();
					for(var i in offlineStocktak) {
						if(offlineStocktak[i]['Id'] == id) {
							offlineStocktak[i]['盘点结果'] = '盘点结束';
						};
					};
					$$(document).trigger('TakeStockTaskItemListPushDone', {
						success: true,
						message: '上传成功！'
					});
					_this.setInventoryAssetList(offlineStocktak, true);
				} else {
					for(i in response) {
						console.log(i + "--" + response[i] + "==pushInventoryAssetList");
					}
					$$(document).trigger('TakeStockTaskItemListPushDone', {
						success: false,
						message: '上传失败！'
					});
				}
			},
			error: function(xhr, status) {
				console.log(status + "-----pushInventoryAssetList");
			}
		});
	};

	window.Base = new Base();
})();